package com.atxuwei.yygh.hosp.controller;

import com.atxuwei.yygh.common.result.Result;
import com.atxuwei.yygh.common.utils.MD5;
import com.atxuwei.yygh.hosp.service.HospitalSetService;
import com.atxuwei.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.atxuwei.yygh.model.hosp.HospitalSet;

import java.util.List;
import java.util.Random;


@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;


    //查询医院表设置表里的所有信息
    @ApiOperation(value="获取所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        return Result.ok(hospitalSetService.list());
    }


    //根据id删除医院设置信息
    @ApiOperation(value="逻辑删除医院设置信息")
    @DeleteMapping("removeHosp{id}")
    public Result removeHospById(@PathVariable long id){
        return hospitalSetService.removeById(id)?Result.ok():Result.fail();
    }


    //条件查询带分页
    @ApiOperation(value="条件查询带分页")
    //current:当前页，limit：每页显示数
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current
                                  , @PathVariable long limit
                                  , @RequestBody(required=false) HospitalSetQueryVo hospitalSetQueryVo){
        //mybatis-plus分页对象
        Page<HospitalSet> page=new Page<>(current,limit);
        //模糊查询条件
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        String hosname=hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.like("hoscode",hoscode);
        }
        //调用分页方法并返回结果
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }


    //添加医院设置
    @ApiOperation(value="添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //状态码 1（可用），0（不可用）
        hospitalSet.setStatus(1);
        //生成随机医院密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //添加医院设置并返回结果
        return hospitalSetService.save(hospitalSet)?Result.ok():Result.fail();
    }

    //根据id获取医院设置
    @ApiOperation(value="根据id获取医院设置")
    @GetMapping("{id}")
    public Result getHospitalById(@PathVariable long id){
        return Result.ok(hospitalSetService.getById(id));
    }

    //修改医院设置
    @ApiOperation(value="修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        return hospitalSetService.updateById(hospitalSet)?Result.ok():Result.fail();
    }

    //批量删除医院设置
    @ApiOperation(value="批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }


    //医院设置锁定和解锁
    @ApiOperation(value="医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSetId = hospitalSetService.getById(id);
        hospitalSetId.setStatus(status);
        hospitalSetService.updateById(hospitalSetId);
        return Result.ok();
    }


    //发送签名密钥
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }


}
